<?php

namespace App\Filament\Resources\ComponentResource\Pages;

use App\Filament\Resources\ComponentResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Database\Eloquent\Model;
use App\Models\ComponentType;
use App\Models\ComponentTypeCode;
use App\Models\Component;

class CreateComponent extends CreateRecord
{
    protected static string $resource = ComponentResource::class;

    protected function mutateFormDataBeforeCreate(array $data): array
    {
       return $data;
    }

    protected function handleRecordCreation(array $data): Model
    {
        $title = $data['title'];
        $description = $data['description'];
        $screenshot = $data['screenshot'];
        $categories = $data['category'];
        $user_id = auth()->id();

        $component = static::getModel()::create([
            'user_id' => $user_id,
            'title' => $title,
            'description' => $description,
            'screenshot' => $screenshot,
            'created_at' => now(),
            'updated_at' => now()
        ]);

        $component->categories()->attach($categories, ['created_at' => now(), 'updated_at' => now()]);

        $types = $data['types'];

        foreach ($types as $type){

            if($type == 'HTML'){

                $type_description = $data['html_type_description'];
                $html = $data['html'];
                $css = $data['css'];
                $js = $data['js'];

                $componentType = ComponentType::create([
                    'name' => $type,
                    'component_id' => $component->id,
                    'description' => $type_description
                ]);

                if(!empty($html)){
                    ComponentTypeCode::create([
                        'name' => 'html',
                        'component_type_id' => $componentType->id,
                        'code' => $html
                    ]);
                }

                if(!empty($css)){
                    ComponentTypeCode::create([
                        'name' => 'css',
                        'component_type_id' => $componentType->id,
                        'code' => $css
                    ]);
                }

                if(!empty($js)){
                    ComponentTypeCode::create([
                        'name' => 'js',
                        'component_type_id' => $componentType->id,
                        'code' => $js
                    ]);
                }
            }

            if($type == 'React'){

                $type_description = $data['react_type_description'];

                $componentType = ComponentType::create([
                    'name' => "React",
                    'component_id' => $component->id,
                    'description' => $type_description
                ]);

                $jsx = $data['jsx'];

                ComponentTypeCode::create([
                    'name' => $type,
                    'component_type_id' => $componentType->id,
                    'code' => $jsx
                ]);
            }

            if($type == 'Vue'){

                $type_description = $data['vue_type_description'];

                $vuejs = $data['VueJS'];

                $componentType = ComponentType::create([
                    'name' => $type,
                    'component_id' => $component->id,
                    'description' => $type_description
                ]);

                ComponentTypeCode::create([
                    'name' => $type,
                    'component_type_id' => $componentType->id,
                    'code' => $vuejs
                ]);
            }

        }

        return $component;
    }
}
